package megajournal.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JOptionPane;
import megajournal.domain.Lab;
import megajournal.domain.Lab_Student;
import megajournal.domain.Lesson;
import megajournal.domain.Student;
import megajournal.view.*;


/**
 *
 * @author cska
 *
 */

public class addAdapter implements ActionListener {

    protected AdminWnd adminWnd;
    protected AdminCtrl adminCtrl;
    protected TeacherWnd teacherWnd;
    protected TeacherCtrl teacherCtrl;
    protected int addMode;

    public static final int ADD_GROUP    =   1;
    public static final int ADD_STUDENT  =   2;
    public static final int ADD_TEACHER  =   3;
    public static final int ADD_SUBJECT  =   4;
    public static final int ADD_GTS      =   5;
    public static final int ADD_ADMIN    =   6;
    public static final int ADD_LESSON   =   7;
    public static final int ADD_SKIP     =   8;
    public static final int ADD_LAB      =   9;
    public static final int ADD_LS       =  10;
    public static final int ADD_INTERIM  =  11;

    public addAdapter(AdminWnd adminWnd, AdminCtrl adminCtrl, int addMode) {
        this.adminWnd = adminWnd;
        this.adminCtrl = adminCtrl;
        this.addMode = addMode;
    }

    public addAdapter(TeacherWnd teacherWnd, TeacherCtrl teacherCtrl, int addMode) {
        this.teacherWnd = teacherWnd;
        this.teacherCtrl = teacherCtrl;
        this.addMode = addMode;
    }

    public void actionPerformed(ActionEvent e) {
        switch (addMode) {
            case ADD_GROUP:
                addGroup();
                break;
            case ADD_STUDENT:
                addStudent();
                break;
            case ADD_TEACHER:
                addTeacher();
                break;
            case ADD_SUBJECT:
                addSubject();
                break;
            case ADD_GTS:
                addGts();
                break;
            case ADD_ADMIN:
                addAdmin();
                break;
            case ADD_LESSON:
                addLesson();
                break;
            case ADD_SKIP:
                addSkip();
                break;
            case ADD_LAB:
                addLab();
                break;
            case ADD_LS:
                addLs();
                break;
            case ADD_INTERIM:
                addInterim();
                break;
        }
    }

    private void addGroup() {
        GroupDlg gd = new GroupDlg(adminWnd, true, "Новая группа", null);
        if (gd.fl) {
            adminCtrl.addGroup(gd.getGroup());
            adminWnd.reloadGroup();
        }
        gd.dispose();
    }

    private void addStudent() {
        if (adminWnd.getGroup() != -1) {
            StudentDlg sd = new StudentDlg(adminWnd, true, "Новый студент", null, adminCtrl.getGroup(adminWnd.getGroup()));
            if (sd.fl) {
                adminCtrl.addStudent(sd.getStudent());
                adminWnd.reloadStudent();
            }
        } else {
            JOptionPane.showMessageDialog(adminWnd, "Не выбрана группа", "Ошибка", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void addTeacher() {
        TeacherDlg td = new TeacherDlg(adminWnd, true, "Новый преподаватель", null);
        if (td.fl) {
            adminCtrl.addTeacher(td.getTeacher());
            adminWnd.reloadTeacher();
        }
        td.dispose();
    }

    private void addSubject() {
        SubjectDlg sd = new SubjectDlg(adminWnd, true, "Новый предмет", null);
        if (sd.fl) {
            adminCtrl.addSubject(sd.getSubject());
            adminWnd.reloadSubject();
        }
        sd.dispose();
    }

    private void addGts() {
        javax.swing.DefaultComboBoxModel group = adminCtrl.groupCombo();
        javax.swing.DefaultComboBoxModel teacher = adminCtrl.teacherCombo();
        javax.swing.DefaultComboBoxModel subject = adminCtrl.subjectCombo();
        if (group.getSize()*teacher.getSize()*subject.getSize() != 0) {
            GtsDlg gd = new GtsDlg(adminWnd, true, "Новое назначение", group, teacher, subject, 0, 0, 0);
            if (gd.fl) {
                adminCtrl.addGts(gd.getGroup(),gd.getTeacher(),gd.getSubject());
                adminWnd.reloadGts();
            }
        }
    }

    private void addAdmin() {
        AdminDlg ad = new AdminDlg(adminWnd, true, "Новый администратор", null);
        if (ad.fl) {
            adminCtrl.addAdmin(ad.getAdmin());
            adminWnd.reloadAdmin();
        }
        ad.dispose();
    }

    private void addLesson() {
        LessonDlg ld = new LessonDlg(teacherWnd, true, "Новое занятие", teacherCtrl.getSubject(teacherWnd.getSubject()), teacherCtrl.getGroup(teacherWnd.getSubject(), teacherWnd.getGroup()));
        if (ld.fl) {
            Lesson lesson = teacherCtrl.addLesson(ld.getLesson());
            teacherCtrl.addSkips(lesson, ld.getStudents());
            teacherWnd.reloadLesson();
        }
        ld.dispose();
    }

    private void addSkip() {
        Lesson lesson = teacherCtrl.getLesson(teacherWnd.getSubject(), teacherWnd.getGroup(), teacherWnd.getLesson(), teacherWnd.getBegin(), teacherWnd.getEnd());
        List<Student> students = teacherCtrl.getStudents(teacherWnd.getSubject(), teacherWnd.getGroup(), teacherWnd.getLesson(), -1, teacherWnd.getBegin(), teacherWnd.getEnd());
        if (students.size() != 0) {
            SkipDlg sd = new SkipDlg(teacherWnd, true, "Новый отсутсвующий", null, lesson, students);
            if (sd.fl) {
                teacherCtrl.addSkip(sd.getSkip());
                teacherWnd.reloadSkip();
            }
            sd.dispose();
        }
    }

    private void addLab() {
        LabDlg ld = new LabDlg(adminWnd, true, "Новое задание", null, teacherCtrl.getSubject(teacherWnd.getSubject_l()));
        if (ld.fl) {
            teacherCtrl.addLab(ld.getLab());
            teacherWnd.reloadLab();
        }
        ld.dispose();
    }

    private void addLs() {
        List<Student> students = teacherCtrl.getStudetns(teacherWnd.getSubject_l(), teacherWnd.getGroup_ls(), teacherWnd.getLab(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l());
        Lab_StudentDlg lsd = new Lab_StudentDlg(teacherWnd, true, "Новые записи", students);
        if (lsd.fl) {
            Lab lab = teacherCtrl.getLab(teacherWnd.getSubject_l(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l(), teacherWnd.getLab());
            teacherCtrl.addLs(lab,lsd.getStudents());
            teacherWnd.reloadLs();
        }
    }

    private void addInterim() {
        Lab_Student ls = teacherCtrl.getLs(teacherCtrl.getLab(teacherWnd.getSubject_l(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l(), teacherWnd.getLab()), teacherCtrl.getGroup(teacherWnd.getSubject_l(), teacherWnd.getGroup_ls()), teacherWnd.getLs());
        InterimDlg id = new InterimDlg(teacherWnd, true, "Новая промежуточная сдача", null, ls);
        if (id.fl) {
            teacherCtrl.addInterim(id.getInterim());
            teacherWnd.reloadInterim();
        }
        id.dispose();
    }

}
